/// [Even Fibonacci numbers](https://projecteuler.net/problem=2)
fn main() {
    for num in &[4000000] {
        println!("{}內所有偶数斐波那契数列的和为{}", num, even_fibonacci_numbers(*num));
    }
}

/// 解法：迭代计算偶数
fn even_fibonacci_numbers(length: u32) -> u32 {
    let mut a = 1;
    let mut b = 2;
    let mut sum = 0;
    while b < length {
        sum += b;
        a = a + b * 2;
        b = a * 2 - b;
    }
    sum
}
